home *** CD-ROM | disk | FTP | other *** search
/ PC Answers 1995 May / PC Answers CD-ROM 7 (Future Publishing) (May 1995).iso / vbits / demos / contempo / microhel / vbitscod.txt < prev   
Encoding:
Text File  |  1994-10-06  |  8.3 KB  |  226 lines

  1.  
  2. Using API Calls During Development
  3. Perhaps the best method of monitoring resources is via VB
  4. itself. By using the Debug window and a few Windows API
  5. functions, you can create a "status" procedure that will
  6. dynamically display the current memory and resource usage as
  7. your application is running. This is ideal for examining the
  8. "before" and "after" effects of loading control-laden forms,
  9. unloading objects, using one control vs. another, allocating
  10. arrays, etc.
  11.  
  12. The following code can be stored in a single code module
  13. called SHOWRES.BAS. Add this module to the project you wish
  14. to analyze, and sprinkle calls to the ShowResources
  15. procedure wherever you wish to check the memory and resource
  16. usage.
  17.  
  18.    ' Add these lines to general section of SHOWRES.BAS:
  19.    Global Const GFSR_GDIResources% = 1
  20.    Global Const GFSR_UserResources% = 2
  21.    Declare Function GetFreeSystemResources Lib "User" (ByVal
  22.    fuSysResource
  23.    _ As Integer) As Integer
  24.    Declare Function GetFreeSpace Lib "Kernel" (ByVal wFlags
  25.    As Integer)
  26.    _ As Long
  27.    ' Add this procedure to SHOWRES.BAS:
  28.    Sub ShowResources ()
  29.      Debug.Print "Free Memory:"; GetFreeSpace(0),
  30.      Debug.Print "GDI:";
  31.    GetFreeSystemResources(GFSR_GDIResources%); "%",
  32.      Debug.Print "User:";
  33.    GetFreeSystemResources(GFSR_UserResources%); "%"
  34.    End Sub
  35.  
  36. Here's a small example to illustrate the ShowResources
  37. procedure:
  38.  
  39.    Sub LoadTest_Click ()
  40.      Debug.Print "Before loading 15 picture controls:"
  41.      ShowResources ' Show statistics in Debug window
  42.      For l = 1 To 15
  43.        Load MyPictures(l)
  44.      Next
  45.      Debug.Print "After loading 15 picture controls:"
  46.      ShowResources ' Show statistics in Debug window
  47.    End Sub
  48.  
  49. The Debug window then shows the following results:
  50.  
  51.    Before loading 15 picture controls:
  52.    Free Memory: 8237312        GDI: 75 %     User: 78 %
  53.    After loading 15 picture controls:
  54.    Free Memory: 8228512        GDI: 67 %     User: 77 %
  55.  
  56. As you can see, the GDI heap took a big hit once the picture
  57. controls were loaded. When the same example is modified to
  58. use image controls instead of picture controls, the Debug
  59. window shows the following:
  60.  
  61.    Before loading 15 image controls:
  62.    Free Memory: 8228416        GDI: 75 %     User: 77 %
  63.    After loading 15 image controls:
  64.    Free Memory: 8225344        GDI: 75 %     User: 77 %
  65.  
  66. The drastic difference in GDI usage proves that image
  67. controls indeed offer significant savings over picture
  68. controls -- assuming that you can do without the additional
  69. features of picture controls (DDE, graphics methods, etc.)
  70.  
  71.  
  72. Using The Graphical ("Light") Controls
  73. Microsoft added the graphical controls (line, shape and
  74. image) in VB 2.0 for good reason. These controls differ from
  75. the other standard controls in that they are not truly
  76. windows -- that's why they lack the hWnd property.
  77. Therefore, they consume far less system resources than their
  78. true-window cousins. (Note: The label control is also a
  79. graphical control but of course is not new to VB 2).
  80.  
  81. For example, you may be currently using a custom control to
  82. display a status gauge like the ones you'll find in your
  83. garden-variety SETUP program. Here's how to make a "poor
  84. man's status gauge" from just two graphical controls and a
  85. few lines of code:
  86.  
  87. 1. Add a label control called StatusLabel, with the
  88.    following property values:
  89.  
  90.    Property    Value
  91.    Alignment   2 (Center)
  92.    BorderStyle 1
  93.  
  94. 2. Add a shape control called StatusBar, with the following
  95.    property values:
  96.  
  97.    Property    Value
  98.    Width       0
  99.    Top         Same as StatusLabel.Top
  100.    Left        Same as StatusLabel.Left
  101.    FillStyle   0 (Solid)
  102.    FillColor   Blue, or color of your choice
  103.    DrawMode    14 (Merge Pen Not)
  104.  
  105. To display a value in the "status bar", use the following
  106. code:
  107.  
  108.    Progress% = some value
  109.    ' The value above should be from 0 to 100
  110.    StatusBar.Width = StatusLabel.Width * (Progress% / 100)
  111.    StatusLabel.Caption = Str$(Progress%) + "%"
  112.  
  113. Another effective use for a graphical control is to use
  114. image controls as buttons.
  115.  
  116.  
  117. Use graphics methods to simulate 3D controls.
  118. Most 3D controls are more than just a pretty face, so
  119. substituting them with a cosmetic-only replacement will
  120. usually rob your application of certain features.
  121. Nevertheless, some 3D controls -- particularly 3D labels and
  122. frames -- are ripe for replacement with simulated 3D
  123. graphics. If you aren't sure how to accomplish a 3D effect
  124. using graphics method only, here's a simple function to get
  125. you started:
  126.  
  127.    ' Draw3DBorder Function: Draws a 3D border around the
  128.    specified
  129.    ' control, with "raised" or "lowered" shading, and user-
  130.    defined
  131.    ' border thickness.
  132.    Sub Draw3DBorder (TargetControl As Control,
  133.    RaisedBorder%, BorderWidth%)
  134.      ' Define how far the 3D lines are drawn from the
  135.      ' outer edges of the control. Modify to your tastes:
  136.      BorderOffset% = 8
  137.    
  138.      ' Define the four corners of the 3D box to draw:
  139.      X1 = TargetControl.Left - BorderOffset%
  140.      Y1 = TargetControl.Top - BorderOffset%
  141.      X2 = X1 + TargetControl.Width + (BorderOffset% * 2)
  142.      Y2 = Y1 + TargetControl.Height + (BorderOffset% * 2)
  143.    
  144.      ' We'll change the parent form's ForeColor and
  145.    DrawWidth
  146.      ' properties, so we'll save them first and restore when
  147.    done
  148.      OriginalForeColor = TargetControl.Parent.ForeColor
  149.      OriginalDrawWidth = TargetControl.Parent.DrawWidth
  150.    
  151.      ' If RaisedBorder% is True, the white lines are drawn
  152.      ' on the top and left sides; otherwise, they are drawn
  153.      ' on the bottom and right sides.
  154.      If RaisedBorder% Then
  155.        UpperColor = QBColor(15)    ' white
  156.        LowerColor = QBColor(8)     ' grey
  157.      Else
  158.        UpperColor = QBColor(8)     ' grey
  159.        LowerColor = QBColor(15)    ' white
  160.      End If
  161.      ' Draw line on left
  162.      TargetControl.Parent.DrawWidth = BorderWidth
  163.      TargetControl.Parent.ForeColor = UpperColor
  164.      TargetControl.Parent.Line (X1, Y2)-(X1, Y1)
  165.      ' Draw line on top
  166.      TargetControl.Parent.Line -(X2, Y1)
  167.      ' Draw line on right
  168.      TargetControl.Parent.ForeColor = LowerColor
  169.      TargetControl.Parent.Line -(X2, Y2)
  170.      ' Draw line on bottom
  171.      TargetControl.Parent.Line -(X1, Y2)
  172.      ' Restore the parent form's original properties
  173.      TargetControl.Parent.ForeColor = OriginalForeColor
  174.      TargetControl.Parent.DrawWidth = OriginalDrawWidth
  175.    End Sub
  176.  
  177. Using the Draw3DBorder function is simply a matter of
  178. specifying the name of the control (TargetControl), whether
  179. you want "raised" lines (RaisedBorder% = True) or "lowered"
  180. lines (RaisedBorder% = False), and the thickness of the 3D
  181. border (BorderWidth%). For example, to draw a 3D border
  182. around a label control, do the following:
  183.  
  184.    ' Draw a raised border around the MyLabel control:
  185.    RaisedBorder% = True
  186.    BorderWidth% = 2
  187.    Draw3DBorder MyLabel, RaisedBorder%, BorderWidth%
  188.  
  189. Note that when using this 3D effect on a form, you should
  190. set the form's AutoRedraw property to True in order to keep
  191. the 3D effect refreshed whenever the form is painted.
  192.  
  193.  
  194. Use SndPlaySound API function instead of MCI.VBX
  195. The MCI control provides a number of convenient services and
  196. sports an attractive cassette deck-style interface, but it
  197. can be overkill if you simply wish to play sound files
  198. without user interaction, or trigger the system event sounds
  199. defined in the Windows Control Panel. The SndPlaySound API
  200. function from the MMSYSTEM.DLL file (included with Windows
  201. 3.1 or the Microsoft Multimedia Extensions for Windows 3.0)
  202. allows you to play sounds without using a custom control. By
  203. avoiding the MCI control, your application will use less
  204. resources and will not require you to distribute MCI.VBX. In
  205. case you were wondering, MCI.VBX also makes calls to
  206. MMSYSTEM.DLL, so using SndPlaySound eliminates the overhead
  207. of the MCI control.
  208.  
  209. The code below shows how to play a WAV audio file with
  210. SndPlaySound:
  211.  
  212.    ' Add these lines to general section:
  213.    Declare Function SndPlaySound Lib "MMSYSTEM.DLL"
  214.    _ (ByVal lpszSoundName$, ByVal wFlags%) As Integer
  215.    ' Flags used by SndPlaySound:
  216.    Const SND_ASYNC = &H0001
  217.    
  218.    ' Play a sound when a button is clicked:
  219.    Sub Command1_Click ()
  220.      wFlags% = SND_ASYNC  ' Play asynchronously (returns
  221.    immediately)
  222.      x% = SndPlaySound("c:\windows\chord.wav", wFlags%)
  223.    End Sub
  224.  
  225.  
  226.